package com.rf.richfitwheel.admin.basedata.controller;

import com.rf.richfitwheel.admin.basedata.service.BasicDataOrgService;
import com.rf.richfitwheel.common.utils.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@RequestMapping("/basicData/dataSyn")
public class BasicDataSyncController {
    private final Logger log = LoggerFactory.getLogger(BasicDataSyncController.class);

    @Autowired
    private BasicDataOrgService basicDataOrgService;

    /**
     * 组织机构同步任务，同步入库+处理
     * @return 处理结果
     */
    @GetMapping("/orgSyn")
    public R orgSyn() {
        try{
            //入库时间
            long myDate = new Date().getTime();
            log.info("开始执行组织机构数据同步................");
            long start = System.currentTimeMillis();
            //同步数据总条数
            Integer countSyn = 0;
            //调用接口同步数据并入库
            countSyn = basicDataOrgService.orgSynDo(myDate, countSyn);
            long end = System.currentTimeMillis();
            log.info("执行组织机构数据同步完成，共同步{}条数据，用时{}秒................", countSyn, (end-start)/1000);
            //处理入库后的数据
            log.info("开始执行组织机构数据更新................");
            long start1 = System.currentTimeMillis();
            basicDataOrgService.orgDeal(myDate);
            long end1 = System.currentTimeMillis();
            log.info("执行组织机构数据更新完成，共用时{}秒................", (end1-start1)/1000);
            return R.ok("执行成功");
        }catch(Exception e){
            log.error("执行失败", e);
            return R.error("执行失败" + e.getMessage());
        }
    }

    /**
     * 组织机构同步任务，已入库的情况下进行处理
     * @param myDate 时间戳毫秒值，同步的时间
     * @return 处理结果
     */
    @GetMapping("/orgDeal/{myDate}")
    public R orgDeal(@PathVariable("myDate") Long myDate){
        try{
            //处理入库后的数据
            log.info("开始执行组织机构数据更新................");
            long start1 = System.currentTimeMillis();
            basicDataOrgService.orgDeal(myDate);
            long end1 = System.currentTimeMillis();
            log.info("执行组织机构数据更新完成，共用时{}秒................", (end1-start1)/1000);
            return R.ok("执行成功");
        }catch(Exception e){
            log.error("执行失败", e);
            return R.error("执行失败" + e.getMessage());
        }
    }

}
